﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Flagwind.ComponentModel
{
	public static class StringExtension
	{
		/// <summary>
		/// 首字母转小写
		/// </summary>
		/// <param name="str"></param>
		/// <returns></returns>
		public static string FirstCharToLower(this string str)
		{
			if(string.IsNullOrEmpty(str))
			{
				return str;
			}

			return str.Substring(0, 1).ToLower() + str.Substring(1);
		}

		/// <summary>
		/// 首字母转大写
		/// </summary>
		/// <param name="str"></param>
		/// <returns></returns>
		public static string FirstCharToUpper(this string str)
		{
			if(string.IsNullOrEmpty(str))
			{
				return str;
			}

			return str.Substring(0, 1).ToUpper() + str.Substring(1);
		}

		/// <summary>
		/// 判断一个字符串是否为合法数字（指定的整数位数和小数位数）
		/// </summary>
		/// <param name="str">待验证的字符串</param>
		/// <param name="precision">整数位数</param>
		/// <param name="scale">小数位数</param>
		/// <returns></returns>
		public static bool IsNumber(string str, int precision, int scale)
		{
			if((precision == 0) && (scale == 0))
				return false;

			string pattern = @"(^\d{1," + precision + "}";

			if(scale > 0)
				pattern += @"\.\d{0," + scale + "}$)|" + pattern;

			pattern += "$)";

			return Regex.IsMatch(str, pattern);
		}
	}
}
